import {
  Alert,
  FlatList,
  StyleSheet,
  Text,
  TextInput,
  TouchableOpacity,
  View,
} from "react-native";
/**
 * Types
 */
import type { AxiosError, AxiosResponse } from "axios";

import Config from "react-native-config";
import axios from "axios";
import store from "store2";

/**
 * Axios
 */
// axios.defaults.baseURL = "http://localhost:8998/api";
// 创建一个 Axios 实例
const api = axios.create({
  baseURL: "http://172.16.31.203:5055", //Config.API_URL, // 替换为你的 API 地址
  timeout: 10000, // 请求超时时间，单位是毫秒
  headers: {
    "Content-Type": "application/json",
  },
});
console.log("API_______________URL:", api);

api.interceptors.request.use((config) => {
  // if (store.has("token")) {
  //   config.headers.Token = store.get("token");
  // }

  return config;
});

api.interceptors.response.use(
  (response: AxiosResponse) => {
    // 用户未登录
    if (response.data.code === 11101) {
      //   message.error("用户未登录");
      Alert.alert("用户未登录");
      store.remove("token");
      store.remove("user");
      //   location.href = "/#/login";
    }

    // 令牌已过期
    if ([11105, 11108].includes(response.data.code)) {
      Alert.alert("令牌已过期");
      store.remove("token");
      store.remove("user");
      //   location.href = "/#/login";
    }

    return response;
  },
  (error: AxiosError) => {
    // message.error(error.message);

    return Promise.reject(error);
  }
);

export default axios;

/**
 * 获取token
 */
// 上面axios封装使用案例
